package server.util;

import com.opencsv.CSVWriter;
import server.enitity.Order;
import server.lunch.AppLunch;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName CSVUtil
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2021/12/20 上午1:05
 * @Version V1.0
 * @Since JDK 11
 */
public class CSVUtil {

    public static void writeCSVData(List<Order> orders) throws IOException {
        FileWriter writer = new FileWriter("/home/melt/java-study/t_order.csv", StandardCharsets.UTF_8);
        CSVWriter csvWriter = new CSVWriter(writer,'\t');
        List<String[]> data  = toStringArray(orders);
        csvWriter.writeAll(data);
        csvWriter.close();
    }

    private static List<String[]> toStringArray(List<Order> orders) {
        List<String[]> records = new ArrayList<String[]>();
        //add header record
        records.add(new String[]{"id","user_id","item_id","count","price_sum","price","pay_price","create_time","update_time","pay_type","order_ip","app_id","app_version","order_no",
        "bussiness_id","status","app_platform","device","item_name","finish_time"});
        Iterator<Order> it = orders.iterator();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
        while(it.hasNext()){
            Order order = it.next();
            records.add(new String[]{order.getId()+"",order.getUserId()+"",order.getItemId()+"",order.getCount()+"",order.getPriceSum()+"",order.getPrice()+"",
            order.getPayPrice()+"",simpleDateFormat.format(order.getCreateTime()),simpleDateFormat.format(order.getUpdateTime()),order.getPayType()+"",order.getOrderIp()+"",order.getAppId()+"",order.getAppVersion()+"",
            order.getOrderNo()+"",order.getBussinessId()+"",order.getStatus()+"",order.getAppPlatform()+"",order.getDevice(),order.getItemName(),order.getFinishTime()+""});
        }
        return records;
    }

    public static void main(String[] args) throws IOException {
        List<Order> orders = AppLunch.buildOrderList(100_0000);
        writeCSVData(orders);
        System.out.println("end");
    }
}
